package com.example.robertluozizhao.framecollectdemo.activity;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.robertluozizhao.framecollectdemo.R;
import com.example.robertluozizhao.framecollectdemo.activity.test.rongcloud.NimMainActivity;
import com.example.robertluozizhao.framecollectdemo.adapter.FlexRecycleAdapter;
import com.example.robertluozizhao.framecollectdemo.db.manager.DbManager;
import com.example.robertluozizhao.framecollectdemo.db.table.User;
import com.example.robertluozizhao.framecollectdemo.util.AppUtils;
import com.example.robertluozizhao.framecollectdemo.util.GlobalFloatManager;


import java.util.ArrayList;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_custom_switchbutton)
    Button btnCustomSwitchbutton;
    @BindView(R.id.btn_bar_chart)
    Button btnBarChart;
    @BindView(R.id.btn_pay)
    Button mBtnPay;
    @BindView(R.id.btn_thread_communication)
    Button btnThreadCommunication;

    private ArrayList<Integer> mDate = new ArrayList<>();
    private FlexRecycleAdapter mFlexRecycleAdapter;
    private String TAG = "MainActivity";


    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
//        LayoutParamsUse();

        Log.d(TAG, "CPU SERIAL:" + getCPUserial());
        DbManager.Companion.getInstance().getBuilder(this);
    }


    public ArrayList<Integer> setData() {
        mDate.clear();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            mDate.add(60 + random.nextInt(160));
        }
        return mDate;
    }


    private void LayoutParamsUse() {
        //        https://www.jianshu.com/p/0d6f753fdd92
        LinearLayout mRootView = (LinearLayout) findViewById(R.id.ll_dongtai);
        LinearLayout mLinearLayout;
        // 第一步，把 LinearLayout 添加到布局里面
        // 给LinearLayoutnew一个LinearLayout.LayoutParams，并且通过  etLayoutParams  让layout更新
        // LayoutParams可以从父窗体获得也可以自己new出来，这里我们采用自己new的方式
        mLinearLayout = new LinearLayout(this);
        mLinearLayout.setBackgroundColor(Color.parseColor("#0000ff"));

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 60);
        mLinearLayout.setLayoutParams(layoutParams);

        mRootView.addView(mLinearLayout);

        // 第二步，把TextView 添加到 第一步的 LinearLayout 里面
        TextView textView = new TextView(this);
        textView.setText("代码在此2");
        textView.setBackgroundColor(Color.parseColor("#ff0000"));
        textView.setGravity(Gravity.CENTER);
        mLinearLayout.addView(textView);

        // 第三步，把TextView获取对应父窗体类型的LayoutParams 并且设置参数更新layout
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(textView.getLayoutParams());
        textParams.width = dip2px(this, 200);
        textParams.height = dip2px(this, 30);
        //textParams.gravity = Gravity.CENTER;  // 无效，这样并不能让文字在控件里居中
        textView.setLayoutParams(textParams);
    }

    private int dip2px(Context context, float dipValue) {
        Resources r = context.getResources();
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics());
    }


    @OnClick({R.id.btn_custom_switchbutton, R.id.btn_custom_slidemenu, R.id.btn_sroll, R.id.btn_inflate,
            R.id.btn_eventbus, R.id.btn_screen_density, R.id.btn_9patch_use, R.id.btn_change_flex,
            R.id.btn_diy_view, R.id.base_adapter, R.id.btn_bar_chart, R.id.btn_touch_or_scroll, R.id.btn_pay,
            R.id.btn_thread_communication, R.id.btn_transaction_progress_diy_view, R.id.btn_fragment_manager,
            R.id.btn_activity_horizontal_scroll, R.id.btn_activity_animation, R.id.btn_camera, R.id.btn_opengl,
            R.id.btn_reflect, R.id.btn_gg_test, R.id.btn_nim_member_list, R.id.btn_global_float_view,
    R.id.btn_touch_delegate,R.id.btn_jniTest,R.id.btn_book})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_custom_switchbutton:
                startActivity(CustomSwitchButtonActivity.class);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        DbManager.Companion.getInstance().getBuilder(MainActivity.this).userDao().insertAll(new User(1,"罗","子朝"));
                    }
                }).start();
                break;
            case R.id.btn_custom_slidemenu:
                startActivity(CustomSlideMenuActivity.class);
                break;
            case R.id.btn_sroll:
                startActivity(CustomScrollDeteleActivity.class);
                break;
            case R.id.btn_inflate:
                startActivity(InflateActivity.class);
                break;
            case R.id.btn_eventbus:
                startActivity(EventActivity.class);
                break;
            case R.id.btn_screen_density:
                /**
                 * taskAffinity 作用
                 */
                Intent intent = new Intent(this, ScreenDesnityActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.btn_9patch_use:
                startActivity(Patch9ImageUseActivity.class);
                break;
            case R.id.btn_change_flex:
                mFlexRecycleAdapter.upDataInfo(setData());
                break;
            case R.id.btn_diy_view:
                startActivity(DiyViewActivity.class);
                break;
            case R.id.base_adapter:
                startActivity(AdapterCheckActivity.class);
                break;
            case R.id.btn_bar_chart:
                startActivity(BarChartActivity.class);
                break;
            case R.id.btn_touch_or_scroll:
                startActivity(TouchOrScrollTestActivity.class);
                break;
            case R.id.btn_pay:
                startActivity(PayActivity.class);
                break;
            case R.id.btn_thread_communication:
                startActivity(ChildThreadCommunicaActivity.class);
                break;
            case R.id.btn_transaction_progress_diy_view:
                startActivity(TransactionProgressDiyActivity.class);
                break;
            case R.id.btn_fragment_manager:
                startActivity(FragmentManagerActivity.class);
            case R.id.btn_activity_horizontal_scroll:
                startActivity(SlidingConflictActivity.class);
                break;
            case R.id.btn_activity_animation:
                startActivity(AnimationActivity.class);
                overridePendingTransition(R.anim.anim_activity_enter, R.anim.anim_activity_out);
                break;
            case R.id.btn_camera:
                startActivity(CameraSimpleUsageActivity.class);
                break;
            case R.id.btn_opengl:
                startActivity(OpenGLES20Activity.class);
                break;
            case R.id.btn_reflect:
                startActivity(ReflectTestActivity.class);
                break;
            case R.id.btn_gg_test:
                startActivity(GgTestActivity.class);
                break;
            case R.id.btn_nim_member_list:
                startActivity(NimMainActivity.class);
                break;
            case R.id.btn_global_float_view:
                createGlobalFloatView();
            case R.id.btn_touch_delegate:
                startActivity(TouchDelegateActivity.class);
                break;
            case R.id.btn_jniTest:
                startActivity(JniTestActivity.class);
                break;
            case R.id.btn_book:
                startActivity(BookPageActivity.class);
        }
    }

    private void createGlobalFloatView() {
        GlobalFloatManager.Companion.getINSTANCE().showFloatView(this);
    }


    private void startActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }


    public String getCPUserial() {
        String serial = "";
        serial = AppUtils.getCPUSerial();
        return serial;
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        GlobalFloatManager.Companion.getINSTANCE().hideFloatView();
    }
}
